package com.adtech.adms.commons.web.tag;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ui.AbstractUITag;

import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.tree.TreeNode;
import com.adtech.adms.commons.util.TreeUtil;
import com.adtech.adms.commons.util.TreeUtil.OnGetNodeListener;
import com.adtech.adms.system.pojo.Feature;
import com.adtech.adms.system.pojo.User;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author Dormin 
 * @since 2011-5-31 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class MainContentTableTag extends AbstractUITag {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4238421068364640444L;

	/**
	 * 模块代码
	 */
	private String featureCode;
	private String id;
	private String title;
	private String functionid;
	private Feature feature;

	public String getFeatureCode() {
		return featureCode;
	}

	public void setFeatureCode(String featureCode) {
		this.featureCode = featureCode;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getFunctionid() {
		return functionid;
	}

	public void setFunctionid(String functionid) {
		this.functionid = functionid;
	}


	@Override
	public Component getBean(ValueStack valueStack, HttpServletRequest request, HttpServletResponse response) {
		User user = (User) request.getSession().getAttribute(SessionKey.SESSION_KEY_USER);
		Map<String,String> contentMap = MainContentTableComponent.getStatisticContentMap().get(user.getUserId());
		if(contentMap!=null) {
			if(contentMap.get(featureCode)!=null) {
				return new MainContentTableComponent(valueStack, request, response);
			}
		}
		List<TreeNode<Feature>> nodes = user.getFeaturesTree();
		TreeNode<Feature> node = null;
		for (TreeNode<Feature> treeNode : nodes) {
			node = TreeUtil.iteratorNodes(treeNode, new OnGetNodeListener<Feature>() {
				public boolean onGetNode(TreeNode<Feature> node) {
					if (featureCode.equals(node.getInnerObject().getCode())) {
						return true;
					}
					return false;
				}
			});
			if (node != null) {
				break;
			}
		}

		return new MainContentTableComponent(valueStack, request, response);
	}

	@Override
	protected void populateParams() {		
		MainContentTableComponent buComponent = (MainContentTableComponent) component;
		buComponent.setFunctionid(functionid);
		buComponent.setTitle(title);
		buComponent.setFeatureCode(featureCode);

	}

}
